﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using System.Windows.Controls;
using Milkify.Diagnostics;
using System.Diagnostics;

namespace Milkify.Input
{
    [Flags]
    public enum BorderType
    {
        Top = 1,
        Left = 2,
        Bottom = 4,
        Right = 8
    }

    public class ResizeHelper
    {
        #region Private Members
        private Window mWindow;
        private Dictionary<FrameworkElement, BorderType> mBorderElements = new Dictionary<FrameworkElement, BorderType>();
        private FrameworkElement mCapturingElement;

        private bool mMouseLeftButtonPressed;
        private bool mIsOverLeftBorder;
        private bool mIsOverRightBorder;
        private bool mIsOverTopBorder;
        private bool mIsOverBottomBorder;
        private Interop.Point mMouseAnchor;
        private Point mWindowAnchor;
        private Size mSizeAnchor;
        #endregion

        #region Construction
        public ResizeHelper(Window window)
        {
            mWindow = window;
        }
        #endregion

        #region Border Elements Methods
        public void AddBorderElement(FrameworkElement element, BorderType borderType)
        {
            mBorderElements.Add(element, borderType);

            if (borderType == BorderType.Bottom || borderType == BorderType.Top)
                element.Cursor = Cursors.SizeNS;
            else if (borderType == BorderType.Left || borderType == BorderType.Right)
                element.Cursor = Cursors.SizeWE;
            else if (borderType == (BorderType.Left | BorderType.Top) || borderType == (BorderType.Right | BorderType.Bottom))
                element.Cursor = Cursors.SizeNWSE;
            else if (borderType == (BorderType.Right | BorderType.Top) || borderType == (BorderType.Left | BorderType.Bottom))
                element.Cursor = Cursors.SizeNESW;

            element.MouseLeftButtonDown += new MouseButtonEventHandler(BorderElement_MouseLeftButtonDown);
            element.MouseLeftButtonUp += new MouseButtonEventHandler(BorderElement_MouseLeftButtonUp);
            element.MouseMove += new MouseEventHandler(BorderElement_MouseMove);
        }

        private void BorderElement_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            mMouseLeftButtonPressed = true;

            FrameworkElement element = (FrameworkElement)sender;
            element.CaptureMouse();
            mCapturingElement = element;

            BorderType borderType = mBorderElements[element];
            mIsOverBottomBorder = ((borderType & BorderType.Bottom) == BorderType.Bottom);
            mIsOverLeftBorder = ((borderType & BorderType.Left) == BorderType.Left);
            mIsOverRightBorder = ((borderType & BorderType.Right) == BorderType.Right);
            mIsOverTopBorder = ((borderType & BorderType.Top) == BorderType.Top);

            Interop.Mouse.GetCursorPos(out mMouseAnchor);
            mWindowAnchor = new Point(mWindow.Left, mWindow.Top);
            mSizeAnchor = new Size(mWindow.ActualWidth, mWindow.ActualHeight);
        }

        private void BorderElement_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            mMouseLeftButtonPressed = false;
            if (mCapturingElement != null)
                mCapturingElement.ReleaseMouseCapture();
        }

        private void BorderElement_MouseMove(object sender, MouseEventArgs e)
        {
            if (mMouseLeftButtonPressed)
            {
                Interop.Point point;
                Interop.Mouse.GetCursorPos(out point);
                double xDiff = (point.X - mMouseAnchor.X);
                double yDiff = (point.Y - mMouseAnchor.Y);
                if (mIsOverRightBorder)
                {
                    mWindow.Width = mSizeAnchor.Width + xDiff;
                }
                if (mIsOverBottomBorder)
                {
                    mWindow.Height = mSizeAnchor.Height + yDiff;
                }
                if (mIsOverLeftBorder)
                {
                    mWindow.Width = mSizeAnchor.Width - xDiff;
                    mWindow.Left = mWindowAnchor.X + xDiff;
                }
                if (mIsOverTopBorder)
                {
                    mWindow.Height = mSizeAnchor.Height - yDiff;
                    mWindow.Top = mWindowAnchor.Y + yDiff;
                }
            }
        }
        #endregion
    }
}
